var co = require('../../../../resource/js/common.js');
var app = getApp()
var _this
Page({
   data: {
      chatInfo: {},   //对话对象的信息
      height: '',     //聊天内容框高度
      userInfo: '',    //自己的用户信息
      msgList: '',      //聊天记录
      bottomHeight: 50,
      top: 1000000,
      centent: ''         //发送的内容
   },
   onLoad: function (options) {
      _this = this;
      var User_ID = options.User_ID;
      this.getChatInfo(User_ID);
      var system = wx.getSystemInfoSync()
      var height = system.windowHeight - 62
      this.setData({
         height: height,
         userInfo: app.Data.userInfo,
      })
      console.log("【我的信息】", app.Data.userInfo)
   },
   //   获取聊天对象信息
   getChatInfo: function (ruid) {
      var param = { User_ID: ruid }
      co.getShareApi('user/Mg/idGetInfo', param, function (res) {
         if (res.code == 0) {
            res.data.User_ID = ruid
            _this.setData({ chatInfo: res.data })
            wx.setNavigationBarTitle({ title: res.data.User_NickName })
            _this.getMsgList(ruid)
            console.log("【聊天对象信息】=======", res.data)
         }
      })
   },
   // 获取聊天记录
   getMsgList: function (ruid) {
      var param = {
         User_ID: app.Data.userInfo.User_ID,
         Receiver_User_ID: ruid
      }
      co.getShareApi('distribute/Account/getMsgList', param, function (res) {
         if (res.code == 0) {
            _this.setData({ msgList: res.data, top: 100000000000 })
            console.log("【聊天记录】", res.data)
         }
      })
   },
   heightChange: function (e) {
      console.log(e.detail.value)
      var query = wx.createSelectorQuery()
      query.select("#bottom").boundingClientRect()
      query.exec(function (res) {
         //res就是 所有标签为mjltest的元素的信息 的数组
         // console.log(res);
         //取高度
         // console.log("【高度】",res[0].height);
         _this.setData({
            bottomHeight: res[0].height,
            top: 10000000000,
            centent: e.detail.value
         })
      })
   },
   //发送消息
   send: function () {
      console.log("【内容】", this.data.centent)
      var param = {
         User_ID: this.data.userInfo.User_ID,
         Receiver_User_ID: this.data.chatInfo.User_ID,
         Mess_Content: this.data.centent
      }
      co.getShareApi("distribute/Account/sendMessage", param, function (res) {
         if (res.code == 0) {
            console.log("发送成功")
            _this.getMsgList(_this.data.chatInfo.User_ID)
            _this.setData({
               centent: ""
            })
         }
      })
   }
})
